/*-----------------------------------------------------------------------------*
A HEAVY HAND OR A HELPING HAND? INFORMATION PROVISION AND CITIZEN PREFERENCES 
FOR ANTI-CRIME POLICIES
PURPOSE: Replicate results 
Corresponding author: Carlos Scartascini (carlossc@iadb.org)
Last Update: May 5, 2021
Software Version: Stata 16 
*-----------------------------------------------------------------------------*/

***********THIS FILE CORRESPONDS TO THE REVISION FOR JPP***********
drop _all
clear all
clear mata
clear matrix
set more off

/*
** INSTRUCTIONS
1. Unzip the data folder in your PC
2. Complete the information in the "DEFINE DIRECTORIES" section, be aware that you need to know your user name to do so. 
3. Install the following packages if your don't have them already

*ssc install texdoc, replace
*ssc install outreg2, replace

4. Run the code
*/

*---------------------------------------*
* 0. DEFINE DIRECTORIES					*
*---------------------------------------*

if "`c(username)'"=="" {
	glo dir 	= ""
	glo data	= "$dir\"
	glo results	= "$dir\"
}

*---------------------------------------*
* 1. OPEN DATASET AND DECLARE ITS TYPE	*
*---------------------------------------*
use "$data\Replication_HH_Preferences_Anticrime.dta", clear 
*svyset upm [pw=wt], strata(estratopri)

*---------------------------------------*
* 2. GLOBALS 							*
*---------------------------------------*

** Dependent variables
global outcomes1  "pen_soc pen_det det_soc"
global outcomes1b  "pen_soc2c pen_soc2d det_soc2c det_soc2d"

** Control variables (balance)
global controls_bal "d_female d_single d_married num_children age ed d_rural  ethn_1 ethn_2 ethn_3 ethn_4 ethn_5 ethn_6 d_employed d_unemployed d_act_hogar d_retired econ_sit d_govassist victim bribe_police d_informed voted d_protest"

** Imbalanced controls
global controls_imb "age ed d_married ethn_2 d_employed d_retired d_rural d_govassist"


*---------------------------------------*
* 3. MAIN RESULTS			 			*
*---------------------------------------*
cd "$results"

/* Figure 3: The Homicides rate in Panama (2000-2017) */
preserve 
use "$data\Homicides_Panama.dta", clear
tsset year

tw (tsline UNODC)(tsline MP_Panama), legend(order(1 "UNODC" 2 "Ministerio Público") size(small)) xlabel(2000(1)2017, labsize(small)) ylabel(5(5)25, labsize(small)) xtitle("") ytitle("Intentional Homicides rate per 100,000 population", size(small)) name(Homicides_Panama, replace)
graph display Homicides_Panama, xsize(20) ysize(12)
graph export "Fig3_Homicides_Panama.pdf", as(pdf) replace font("Helvetica")
restore


/* Table 1: Impact of Crime Information on Policy Preferences */

foreach y of global outcomes1 {
	reg `y' ITT1 ITT2, vce(cluster upm)
	local con_mean : di %7.2f _b[_cons]
	test ITT1=ITT2
	local tto1tto2=r(p)
	outreg2 using "Table1_MainResults", tex append keep(ITT1 ITT2) slow(100) label addstat(p-value T1=T2, `tto1tto2', Control mean, `con_mean') addtext(Controls, No) dec(3) addnote("Notes: Table displays the estimate of OLS regression models when outcome Y of individual i is regressed on the treatments and a set of covariates. Each column in the table corresponds to a different specification. First column in each set has no controls. Narrow corresponds to a set of controls that showed some imbalance. Broad corresponds to the full set of covariates shown in the balance table in Appendix B. Clustered standard errors (at the primary sampling unit) in parentheses. *** p<0.01, ** p<0.05, * p<0.1")
	
	reg `y' ITT1 ITT2 $controls_imb, vce(cluster upm)
	test ITT1=ITT2
	local tto1tto2=r(p)
	outreg2 using "Table1_MainResults", tex append keep(ITT1 ITT2) slow(100) label addstat(p-value T1=T2, `tto1tto2', Control mean, `con_mean') addtext(Controls, Narrow) dec(3)
	
	reg `y' ITT1 ITT2 $controls_bal, vce(cluster upm)
	test ITT1=ITT2
	local tto1tto2=r(p)
	outreg2 using "Table1_MainResults", tex  append keep(ITT1 ITT2) slow(100) label addstat(p-value T1=T2, `tto1tto2', Control mean, `con_mean') addtext(Controls, Broad) dec(3) 	
}	

/* Figure 5: Impact of Information Treatments on Relative Preference for Policies (by Level of News Consumption) */

cap drop x y 
gen x = runiform(-0.02,0.02)
gen y = runiform(-0.05,0.05)

foreach v of global outcomes1 {
scatter y x, name(`v'N, replace) msymbol(none)  /// 
  xlabel(,nogrid) ylabel(,nogrid) yscale(off) xscale(off) /// 
  text(0 0 "`: variable label `v''" ,size(medsmall) place(c) orient(vertical)) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) fxsize(3) fysize(100) 
}

set more off
foreach y of global outcomes1 { 
reg `y' ITT1##i.d_informed ITT2##i.d_informed $controls_imb , vce(cluster upm)
margins, dydx(ITT1) at(d_informed =(0 1)) 
marginsplot, plotopts(connect(none)) legend(off) l(95) addplot(line cero eje2, xlabel(0 1)) yscale(range(-1.5(0.5)5)) title("T1: Increasing Homicides")  xtitle("Informed") ytitle("Treatment Effect") name(T1, replace)

margins, dydx(ITT2) at(d_informed =(0 1)) 
marginsplot, plotopts(connect(none)) legend(off) l(95) addplot(line cero eje2, xlabel(0 1)) yscale(range(-1.5(0.5)5)) title("T2: Decreasing Homicides")  xtitle("Informed") ytitle("Treatment Effect") name(T2, replace)

graph combine T1 T2, ycom r(1) name(`y', replace)
}		

graph combine pen_socN pen_soc pen_detN pen_det det_socN det_soc, ycom c(2) note("Note: 95% Confidence Interval", size(vsmall)) name(margins, replace) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
graph display margins, xsize(17.5) ysize(20)
graph export "Fig5_ResultsInformed.pdf", as(pdf) replace font("Helvetica")


*---------------------------------------*
* 4. APPENDIX 							*
*---------------------------------------*

/* Table B1: Descriptive Statistics and Balance Table */

if 1==1{
cap texdoc close
	texdoc init TableB1_Descriptives_Balance, replace force
	tex \begin{table}[H]
	tex \centering
	tex \scriptsize		
	tex \caption{Descriptive Statistics and Balance Table\label{tab:Descriptives_Balance}}
	tex \begin{tabular}{l*{5}{c}}			
	tex \toprule
	tex 					& Control & \multicolumn{2}{c}{Difference w.r.t. control}	& p-value Wald test  & Observations\\
	tex \textbf{Variable}  	& (av. \& s.d.) & T1 	& T2 	& T1=T2\\
	tex \cmidrule(lr){2-2} \cmidrule(lr){3-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6}
	tex & (1) & (2) & (3) & (4) & (5) \\
	tex \midrule
	foreach v in $controls_bal{
	disp in red "`v'"
		
		reg `v' ITT1 ITT2, vce(cluster upm)
		loc meT0`v'	: di %7.3f _b[_cons]
		loc seT0`v'	: di %7.3f _se[_cons]
		loc diT1`v' : di %7.3f _b[ITT1]
		loc seT1`v' : di %7.3f _se[ITT1]
		loc diT2`v' : di %7.3f _b[ITT2]
		loc seT2`v' : di %7.3f _se[ITT2]
		
		loc tbef1 = _b[ITT1]/_se[ITT1]
		loc pbef1 : di %7.3f 2*ttail(e(df_r),abs(`tbef1'))	
		loc staru1 = ""
		if ((`pbef1' < 0.1))  loc staru1 = "*" 
		if ((`pbef1' < 0.05)) loc staru1 = "**" 
		if ((`pbef1' < 0.01)) loc staru1 = "***" 
		
		loc tbef2 = _b[ITT2]/_se[ITT2]
		loc pbef2 : di %7.3f 2*ttail(e(df_r),abs(`tbef2'))	
		loc staru2 = ""
		if ((`pbef2' < 0.1))  loc staru2 = "*" 
		if ((`pbef2' < 0.05)) loc staru2 = "**" 
		if ((`pbef2' < 0.01)) loc staru2 = "***" 
			
		test ITT1=ITT2
		loc t1t2`v'	  : di %7.3f r(p)
		
		loc n`v' : di %7.0f e(N)
	
		tex \parbox[l]{3.5cm}{ `:variable label `v''} 	& `meT0`v'' 		& `diT1`v''`staru1' 	& `diT2`v''`staru2' & `t1t2`v'' & `n`v''\\
		tex                           				  	&  (`seT0`v'') 		& (`seT1`v'')	& (`seT2`v'') 	&			  & 	  \\
	}	
	tex \addlinespace[2pt] 
	tex \bottomrule
	tex \addlinespace[2pt]
	tex \multicolumn{6}{c}{\footnotesize{\begin{minipage}{0.85\textwidth}\textit{Notes:} Each row shows summary statistics for a different variable. Column (1) shows the sample average and standard deviation in parenthesis. Columns (2) and (3) shows regression coefficients and clustered standard errors (primary sample unit) corresponding to an OLS regression. The p-value in column (4) corresponds to a Wald test of equality of coefficients. Column (5) shows the sample size. The indicator function 1(.) equals to one if the condition inside the parenthesis is true and zero otherwise. *, **, and *** denote statistical significance at the 10, 5 and 1 percent level, respectively.\end{minipage}}}
	tex \end{tabular}
	tex \end{table}
	texdoc close	
}

/* Table B2: Impact of Crime Information on Policy Preferences - individual social policy components */

foreach y of global outcomes1b {
	reg `y' ITT1 ITT2, vce(cluster upm)
	local con_mean : di %7.2f _b[_cons]
	test ITT1=ITT2
	local tto1tto2=r(p)
	outreg2 using "TableB2_SocialPolicies", append slow(100) keep(ITT1 ITT2) label addstat(p-value T1=T2, `tto1tto2', Control mean, `con_mean') addtext(Controls, No) dec(3) addnote("Notes: Table displays the estimate of OLS regression models when outcome Y of individual i is regressed on the treatments and a set of covariates. Each column in the table corresponds to a different specification. First column in each set has no controls. Broad corresponds to the full set of covariates. Clustered standard errors (at the primary sampling unit) in parentheses. *** p<0.01, ** p<0.05, * p<0.1")
	
	reg `y' ITT1 ITT2 $controls_bal, vce(cluster upm)
	test ITT1=ITT2
	local tto1tto2=r(p)
	outreg2 using "TableB2_SocialPolicies", append slow(100) keep(ITT1 ITT2) label addstat(p-value T1=T2, `tto1tto2', Control mean, `con_mean') addtext(Controls, Broad) dec(3)
}

/* Figure C1: Distribution of coins - control group */

graph box  pen_soc pen_det det_soc Punishment Detection Social if ITT3==1
graph export "FigC1_DistributionCoins_control.pdf", as(pdf) replace font("Helvetica")

/* Figure C2: Distribution of coins - treatment 1 vs control group */

if 1==1{
* Penalties histogram
twoway (hist Punishment if ITT3 == 1, discrete frac lcolor(gs12) fcolor(gs12)) ///
   (hist Punishment if ITT2 == 1, discrete frac fcolor(none) lcolor(red)), ///
   title("Control (grey) vs Treatment 1 (red)", size(small)) ///
   ytitle(, size(small)) ylabel(, labsize(vsmall)) ///
   legend(off) xlabel(0(1)10,labsize(vsmall)) xtitle(Coins Assigned to Punishment, size(small)) name(Penalties, replace)

* Social histogram
twoway (hist Social if ITT3 == 1, discrete frac lcolor(gs12) fcolor(gs12)) ///
   (hist Social if ITT2 == 1, discrete frac fcolor(none) lcolor(red)), ///
   title("Control (grey) vs Treatment 1 (red)", size(small)) ///
   ytitle(, size(small)) ylabel(, labsize(vsmall)) ///
   legend(off) xlabel(0(1)10,labsize(vsmall)) xtitle(Coins Assigned to Social Policies, size(small)) name(Social, replace)

graph combine Penalties Social, c(1) name(FigC2, replace) ycom
graph display FigC2, xsize(15) ysize(20)
graph export "FigC2_DistributionCoins_controltreatment1.pdf", as(pdf) replace font("Helvetica")
}


/* Figure C3: The Homicide Rate in Central America */
use "$data\Homicides_CAM.dta", clear

graph bar homicides, over(country) ytitle("Intentional homicides rate per 100,000 population", size(small)) ylabel(0(20)120, labsize(small)) blabel(bar, position(outside) format(%9.1f)) name(Homicides_CAM, replace)
graph display Homicides_CAM, xsize(20) ysize(12)
graph export "FigC3_HomicideRate_CAM.pdf", as(pdf) replace font("Helvetica")

/* Figure C4: Share of people considering Crime and Violence as the main problem in the country over time */
use "$data\Crime_Main_Problem.dta", clear

graph bar crime_mainproblem, over(year) ytitle("Share of people", size(small)) ylabel(0(10)60, labsize(small)) blabel(bar, position(outside) format(%9.1f)) name(Crime_Main_Problem, replace)
graph display Crime_Main_Problem, xsize(20) ysize(12)
graph export "FigC4_Crime_Main_Problem.pdf", as(pdf) replace font("Helvetica")


cap drop cero eje* x y
